package com.cee.admin.modules.prod.steel.controller;

import com.cee.admin.common.BaseProdController;
import com.cee.common.utils.ArithmeticUtils;
import com.cee.common.web.result.Result;
import com.cee.domain.modules.prod.steel.bo.ProductCalculateEmissionsSteelBo;
import com.cee.domain.modules.prod.steel.vo.ProdProductSteelFillDataVo;
import com.cee.domain.modules.prod.steel.vo.ProdProductSteelRespVo;
import com.cee.domain.modules.prod.vo.ProdProductDataReportVo;
import com.cee.ds.common.utils.product.ProductCalculateEmissionsSteelUtil;
import com.cee.ds.modules.prod.steel.service.IProdProductSteelBaseService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import static com.cee.domain.constants.DomainConstants.COMMON_UNAUDITED_STATUS;
import static com.cee.domain.constants.DomainConstants.COMMON_UNSUBMITTED_AUDIT_STATUS;

/**
 * <p>
 * 钢铁产品数据填报基本信息 前端控制器
 * </p>
 *
 * @author autoGenerator
 * @since 2024-04-01
 */
@Tag(name = "产品管理-产品-钢铁产品数据填报基本信息")
@RestController
@RequestMapping("/api/prod/product/steel/base")
public class ProdProductSteelBaseController extends BaseProdController {

    @Autowired
    private IProdProductSteelBaseService prodProductSteelBaseService;

    @Operation(summary = "保存钢铁产品数据填报")
    @PostMapping("/save")
    public Result<Long> save(@RequestBody ProdProductSteelFillDataVo prodFillVo){
        checkProductPermWithCity(prodFillVo.getDataReport().getProductId());
        prodProductSteelBaseService.saveOrSubmitWithAdmin(prodFillVo, COMMON_UNSUBMITTED_AUDIT_STATUS);
        return success(prodFillVo.getDataReport().getId());
    }

    @Operation(summary = "提交钢铁产品数据填报")
    @PostMapping("/submit")
    public Result<Boolean> submit(@RequestBody ProdProductSteelFillDataVo prodFillVo){
        checkProductPermWithCity(prodFillVo.getDataReport().getProductId());
        ProdProductDataReportVo dataReport = prodFillVo.getDataReport();
        ProductCalculateEmissionsSteelBo bo = prodProductSteelBaseService.handleCalculateData(prodFillVo);
        Double emissions = ProductCalculateEmissionsSteelUtil.calculateEmissions(bo);
        dataReport.setProductTotalYield(bo.getALg());
        dataReport.setUnitProductEmissions(emissions);
        dataReport.setTotalEmissions(ArithmeticUtils.multiply(bo.getALg(),emissions).doubleValue());
        return success(prodProductSteelBaseService.saveOrSubmitWithAdmin(prodFillVo, COMMON_UNAUDITED_STATUS));
    }

    @Operation(summary = "查看钢铁产品数据填报详情")
    @GetMapping("/get/{id}")
    public Result<ProdProductSteelRespVo> get(@PathVariable("id") @Parameter(description = "数据填报id")Long id){
        checkReportPermWithCity(id);
        return success(prodProductSteelBaseService.getFillDataByIdWithAdmin(id));
    }

    @Operation(summary = "删除钢铁产品数据填报")
    @DeleteMapping("/delete/{id}")
    public Result<Boolean> delete(@PathVariable("id")@Parameter(description = "数据填报id") Long id){
        checkReportPermWithCity(id);
        return success(prodProductSteelBaseService.removeFillDataByIdWithAdmin(id));
    }


}
